<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="店铺管理">
<style>
    #back {
        display: block;
        margin-left: 30px;
        padding-top: 10px !important;
    }
     .form-horizontal .controls {
       line-height: 40px;
       height: 40px;
       margin-left: 3px;
     }
    .button-danger {
      margin-left:25px;
    }
</style>
<!--suppress ALL -->
<form id="searchForm" class="form-panel"  action="${basepath}/rest/manage/store/selectSeatPage">
    <a href="${basepath}/rest/manage/store/toList" id="back">返回上一级</a>
    <ul class="panel-content">
        <li>
            <div class="control-group span5">
                <div class="search-controls  controls">
                    <input type="text" name="number" id="number" value="" placeholder="搜索关键字">
                </div>
            </div>
            <div class="form-actions span2">
                <button type="submit" class="button  button-primary">
                    搜索
                </button>
            </div>
            <div class="form-actions">
                <a href="javascript:add()" class="button button-success">添加
                </a>
                <a class="button button-danger" href="javascript:delFunction()">批量删除
                </a>
                <a href="javascript:queryIcon()" class="button button-primary">查看菜单二维码
                </a>
            </div>
        </li>
    </ul>
</form>
<div id="grid"></div>
<#----------------------------------------------新增表单start--------------------------------------------------------->
<div id="addContent" style="display:none;">
    <form id="addForm" class="form-horizontal" action="${basepath}/rest/manage/store/insertSeat" method="post">
        <input type="hidden" class="input-normal control-text" name="storeID" id="addStoreID" value=""/>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">餐桌类型：</label>

                <div class="controls control-row4">
                    <select data-rules="{required:true}" name="type">
                        <option value="">-请选择餐桌类型-</option>
                        <option value="big">大桌</option>
                        <option value="middle">中桌</option>
                        <option value="small">小桌</option>
                    </select>
                </div>
            </div>
            <div class="control-group span16">
                <label class="control-label">餐桌编号：</label>

                <div class="controls">
                    <input type="text" class="input-normal control-text number" name="number" data-rules="{required:true}">
                </div>
            </div>

        </div>
    </form>
</div>
<#----------------------------------------------新增表单 end---------------------------------------------------------->
<#----------------------------------------------编辑表单start--------------------------------------------------------->
<div id="editContent" style="display:none;">
    <form id="editForm" class="form-horizontal" action="${basepath}/rest/manage/seat/updateJson" method="post">
        <input type="hidden" class="input-normal control-text" name="id"/>
        <div class="row">
            <div class="row">
                <div class="control-group span16">
                    <label class="control-label">餐桌类型：</label>

                    <div class="controls control-row4">
                        <select data-rules="{required:true}" name="type">
                            <option value="">-请选择餐桌类型-</option>
                            <option value="big">大桌</option>
                            <option value="middle">中桌</option>
                            <option value="small">小桌</option>
                        </select>
                    </div>
                </div>
                <div class="control-group span16">
                    <label class="control-label">餐桌编号：</label>
                    <div class="controls">
                        <input type="text" class="input-normal control-text number1" name="number" data-rules="{required:true}" >
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<#----------------------------------------------查看菜单二维码start--------------------------------------------------------->
<div id="queryContent" style="display:none;">
    <form id="queryForm" class="form-horizontal">
        <div class="row">
            <div style="text-align: center;height: auto;">
                <img style="max-width: 250px;width: 100%;max-height: 250px;" id="menuImg">
            </div>
        </div>
    </form>
</div>
<#----------------------------------------------查看菜单二维码end--------------------------------------------------------->
<script>
    var storeID = getQueryString('id');

    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title: '序号', dataIndex: 'id', elCls: 'center', width: '5%'},
                {
                    title: '餐桌类型', dataIndex: 'type', elCls: 'center', width: '15%', renderer: function (value) {
                    switch (value) {
                        case 'big':
                            return '大桌';
                        case 'middle':
                            return '中桌';
                        case 'small':
                            return '小桌';
                    }
                }
                },
                {title: '编号', dataIndex: 'number', elCls: 'center', width: '15%'},
                {
                    title: '二维码', dataIndex: 'qrCode', elCls: 'center', width: '20%', renderer: function (value) {
                    return '<img src="/' + value + '" style="width:110px;height:110px" />';
                }
                },
                {
                    title: '当前状态', dataIndex: 'status', elCls: 'center', width: '15%', renderer: function (value) {
                    switch (value) {
                        case 'bind':
                            return '就餐中';
                        case 'unBind':
                            return '空台';
                    }
                }
                },
                {title: '就餐单号', dataIndex: 'orderID', elCls: 'center', width: '15%', renderer: function (value) {
                   if(value==''||value==null){
                       return '无';
                   }else{
                       return value;
                   }
                }
                },
                {
                    title: '操作',
                    dataIndex: 'id',
                    elCls: 'center',
                    width: '10%',
                    renderer: function (value, data) {
                        var option1 = '<a href="javascript:edit(' + value + ')">编辑</a>';
                        var option2 = '<a href="javascript:deleteOne(' + value + ')">删除</a>';
                        return option1 + "&nbsp;&nbsp;&nbsp;&nbsp;" + option2;
                    }
                }
            ];
    //分页查询
    var store = new Store({
                url : '/rest/manage/store/selectSeatPage',
                autoLoad:true, //自动加载数据
                params : { //配置初始请求的参数
                    length : '10',
                    storeID: ${e.id!}
                },
                pageSize:10,	// 配置分页数目
                root : 'list',
                totalProperty : 'total'
            }),
            grid = new Grid.Grid({
                render: '#grid',
                columns: columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            });

    grid.render();
    /*--------------------------------模糊查询---------------------------------------------*/
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
    /*--------------------------------模糊查询---------------------------------------------*/
    /*--------------------------------删除用户---------------------------------------------*/
    //删除选中的记录
    function delFunction() {
        BUI.Message.Confirm('确认要删除吗？', function () {
            var selections = grid.getSelection();
            var ids = new Array();
            for (var i = 0; i < selections.length; i++) {
                ids[i] = selections[i].id.toString()
            }
            $.ajax({
                type: "POST",
                url: "${basepath}/rest/manage/seat/deletesJson",
                dataType: "json",
                data: {
                    ids: ids
                },
                success: function (data) {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }
    //删除单个用户
    function deleteOne(value) {
        BUI.Message.Confirm('确认要删除吗？', function () {
            $.ajax({
                type: 'POST',
                url: '${basepath}/rest/manage/seat/deleteJson',
                dataType: 'json',
                data: {
                    id: value
                },
                success: function () {
                    var obj = form.serializeToObject();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }
    /*--------------------------------删除用户---------------------------------------------*/
    /*------------------------------------------------新增表单处理start------------------------------------------------*/

    var addForm = new BUI.Form.Form({
        srcNode: '#addForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            addDialog.close();
        }
    }).render();
    var usernameField = addForm.getField('number');
    usernameField.set('remote', {
        url: '${basepath}/rest/manage/seat/selectDetail',
        dataType: 'json',//默认为字符串
        callback: function (data) {
            if (data.data == null) {
                return '';
            } else {
                return "编号已存在!";
            }
        }
    });
    var addDialog = new BUI.Overlay.Dialog({
        title: '新增餐桌',
        width: 500,
        contentId: 'addContent',
        success: function () {
            if (addForm.isValid()) {
                addForm.ajaxSubmit();
            } else {
                addForm.valid();
            }
        }
    });
    //添加按钮事件
    function add() {
        addForm.clearFields();
        $("#addStoreID").val(storeID);
        addDialog.show();
    }
    /*------------------------------------------------新增表单处理 end-------------------------------------------------*/
    /*------------------------------------------------编辑表单处理 start-----------------------------------------------*/
    var editForm = new BUI.Form.Form({
        srcNode: '#editForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            editDialog.close();
        }
    });
    editForm.render();
    var usernameField = editForm.getField('number');
    usernameField.set('remote', {
        url: '${basepath}/rest/manage/seat/selectAllList',
        dataType: 'json',//默认为字符串
        callback: function (data) {
            var num=$('.number1').val();
            var list=data.data;
            $.each(list,function (i, e) {
                if(num==e.number){
                    return "用户已存在!";
                }
            });
        }
    });
    var editDialog = new BUI.Overlay.Dialog({
        title: '编辑餐桌',
        width: 500,
        contentId: 'editContent',
        success: function () {
            if (editForm.isValid()) {
                editForm.submit();
            } else {
                editForm.valid();
            }
        }
    });

    //编辑按钮事件
    function edit(id) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/rest/manage/seat/toEditJson",
            data: {id: id},
            success: function (data) {
                var form = $("#editForm")[0];
                BUI.FormHelper.setFields(form, data.data);
                editDialog.show();
            }
        });
    }
    /*------------------------------------------------编辑表单处理 end------------------------------------------------*/
    /*------------------------------------------------查询菜单二维码 start-----------------------------------------------*/
    var queryForm = new BUI.Form.Form({
        srcNode: '#queryForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            queryDialog.close();
            $(".bui-stdmod-footer").show();//显示弹出框的确定与取消
        }
    });
    queryForm.render();

    var queryDialog = new BUI.Overlay.Dialog({
        title: '菜单二维码',
        width: 400,
        contentId: 'queryContent',
        success: function () {

        }
    });

    //查询入股店铺
    function queryIcon() {
        $.ajax({
            type: "GET",
            dataType: "json",
            url: "/rest/manage/store/selectDetail",
            data:{
                id:${e.id!}
            },
            success: function (data) {
                var imgIcon='/'+data.data.qrCode;
                $("#menuImg").attr('src',imgIcon);
                queryDialog.show();
                $(".bui-stdmod-footer").hide();//隐藏弹出框的确定与取消
            }
        });
    }
    /*------------------------------------------------查询菜单二维码 end-----------------------------------------------*/
</script>

</@page.pageBase>
